package com.noble.glamour.ui.pay;

import android.support.v4.app.FragmentActivity;

import com.noble.glamour.module.util.BaseUtil;
import com.noble.library.utils.UIUtil;
import com.noble.library.observe.MsgMgr;

import java.util.Timer;
import java.util.TimerTask;

/**
 * 支付结果检测
 */
public class PayCompleteCheckMgr {
    private final FragmentActivity activity;
    private final boolean closeAct;
    private Timer timer;

    public PayCompleteCheckMgr(FragmentActivity activity, boolean closeAct) {
        this.activity = activity;
        this.closeAct = closeAct;
    }

    public void startTimer() {
        cancelTimer();

        timer = new Timer();
        timer.schedule(new PayCompleteCheckTask(), 500, 500);
    }

    public void cancelTimer() {
        try {
            if (timer != null) timer.cancel();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private class PayCompleteCheckTask extends TimerTask {
        @Override
        public void run() {
            MsgMgr.getInstance().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (UIUtil.isActDestroyed(activity)) return;
                    if (!BaseUtil.isTopActivity(activity, activity.getClass().getName())) return;

                    cancelTimer();
                    PayConst.checkPayComplete(activity);

                    if (closeAct) activity.finish();
                }
            });
        }
    }
}